<template>
  <el-container class="home_container">
    <!-- 头部 -->
    <el-header>
      <div>
        <router-link to="/Welcome"><img src="../assets/logo.png" alt=""></router-link>
        
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主题 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '300px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
      class="el-menu-vertical-demo"
      background-color="#cce9fc"
      text-color="#333 "
      active-text-color="#409BFF" :unique-opened="true" :collapse="isCollapse" 
      :collapse-transition="false" :router="true" :default-active="activePath">
      <!-- 一级菜单 -->
      <el-submenu :index="item.id+''" v-for="(item,index) in menulist" :key="index">
        <!-- 一级菜单模板区 -->
        <template slot="title">
          <i :class="icosObj[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
        <el-menu-item :index="'/'+sumitem.path + ''" v-for="(sumitem,index) in item.children" :key="index"
        @click="saveNavState('/'+sumitem.path)">
          <template slot="title">
          <i class="el-icon-menu"></i>
          <span>{{sumitem.authName}}</span>
        </template>
        </el-menu-item>
       </el-submenu>

        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menulist:[],
      icosObj:{
        '125':'iconfont icon-users',
        '103':'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao'
      },
      isCollapse:false,
      activePath:'',
    }
  },
  created(){
    this.getMeunList()
    this.activePath=window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
   async getMeunList(){
      const {data:res} = await this.$http.get('menus')
      console.log(res)
      if(res.meta.status!==200) return this.$message.error(res.meta.msg)
      this.menulist=res.data
    },
    toggleCollapse(){//菜单折叠
      this.isCollapse =!this.isCollapse
    },
    saveNavState(val){
      window.sessionStorage.setItem('activePath',val)
      this.activePath=val
    },
  },
}
</script>
<style  scoped>
.home_container{
  height: 100%;
}
 .el-header {
    background-color: #03a9f4;
    color: #fff;
     display: flex;
     justify-content: space-between;
    align-items: center;
  }
  .el-header img{
    width: 60px;
    height: 60px;
  }
  .el-header>div{
    display: flex;
    align-items: center;
  }
   .el-header div span{
     margin-left: 15px;
     font-size: 19px;
   }

  .el-aside {
    background-color: #cce9fc;
    color: #333;
  }
  
  .el-main {
    background-color: #ebebeb;
    color: #333;
    
  }
  .el-menu{ 
    border-right:0 ;
  }
  body > .el-container {
    margin-bottom: 40px;
  }
  .iconfont{
    margin-right: 10px;
  }
 .toggle-button{
   background-color: #7ec2f9;
   color: white;
   font-size: 18px;
   text-align: center;
   letter-spacing: 0.2em;
   line-height: 35px;
   cursor: pointer;
 }
</style>